package com.hh.demo;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录界面
 * @author 小曾
 */
public class Login extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    //定义组件
    JButton jb1,jb2=null;
    JRadioButton jrb1,jrb2=null;
    JPanel jp1,jp2,jp3,jp4,jp5=null;
    JTextField jtf,yjtf,sjtf=null;
    JLabel jlb1,jlb2,jlb3,jlb4=null;
    JPasswordField jpf=null;
    ButtonGroup bg=null;

    //设定用户名和密码
    final String stu_name="stu";
    final String stu_pwd="111";
    final String tea_name="tea";
    final String tea_pwd="666";
    String yzm="";

    public Login()
    {
        //创建组件
        jb1=new JButton("登录");
        jb2=new JButton("重置");
        //设置监听
        jb1.addActionListener(this);
        jb2.addActionListener(this);

        jrb1=new JRadioButton("教师");
        jrb2=new JRadioButton("学生");
        bg=new ButtonGroup();
        bg.add(jrb1);
        bg.add(jrb2);
        jrb2.setSelected(true);

        jp1=new JPanel();
        jp2=new JPanel();
        jp3=new JPanel();
        jp4=new JPanel();
        jp5=new JPanel();

        jlb1=new JLabel("用户名：");
        jlb2=new JLabel("密    码：");
        jlb3=new JLabel("权    限：");
        jlb4=new JLabel("验证码：");

        jtf=new JTextField(10);
        jpf=new JPasswordField(10);
        yjtf=new JTextField(5);
        sjtf=new JTextField(4);
        //验证码功能
        int i=4;
        String s="";
        while(i-->0) {
            if(i%2==0) {
                int x=(int) (Math.random()*10);
                yzm=yzm+String.valueOf(x);
                s=s+String.valueOf(x)+" ";
            }
            else {
                char c=(char) ((97+(int) (Math.random()*26)));
                yzm=yzm+String.valueOf(c);
                s=s+String.valueOf(c)+" ";
            }
        }
        sjtf.setText(s);

        //加入到JPanel中
        jp1.add(jlb1);
        jp1.add(jtf);

        jp2.add(jlb2);
        jp2.add(jpf);

        jp3.add(jlb4);
        jp3.add(yjtf);
        jp3.add(sjtf);

        jp4.add(jlb3);
        jp4.add(jrb1);
        jp4.add(jrb2);

        jp5.add(jb1);
        jp5.add(jb2);

        //加入JFrame中
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);
        this.add(jp4);
        this.add(jp5);
        //设置布局管理器
        this.setLayout(new GridLayout(5,1));
        //给窗口设置标题
        this.setTitle("登录系统");
        //设置窗体大小
        this.setSize(300,220);
        //设置窗体初始位置
        this.setLocation(600, 250);
        //设置当关闭窗口时，保证JVM也退出
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示窗体
        this.setVisible(true);
        this.setResizable(true);

    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand()=="登录")
        {
            //如果选中教师登录
            if(jrb1.isSelected())
            {
                tealogin();
            }else if(jrb2.isSelected()) //学生在登录系统
            {
                stulogin();
            }
        }else if(e.getActionCommand()=="重置")
        {
            clear();
            clear1();
        }

    }

    //学生登录判断方法
    @SuppressWarnings("deprecation")
    public void stulogin()
    {
        if(stu_name.equals(jtf.getText())&&stu_pwd.equals(jpf.getText())&&yzm.equals(yjtf.getText()))
        {
//			System.out.println("登录成功");
            JOptionPane.showMessageDialog(null,"登录成功！","提示消息",JOptionPane.WARNING_MESSAGE);
            clear();
            dispose();
            new Main();
        }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(null,"请输入用户名和密码！","提示消息",JOptionPane.WARNING_MESSAGE);
        }else if(jtf.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(null,"请输入用户名！","提示消息",JOptionPane.WARNING_MESSAGE);
        }else if(jpf.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(null,"请输入密码！","提示消息",JOptionPane.WARNING_MESSAGE);
        }
        else if(yjtf.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(null,"请输入验证码！","提示消息",JOptionPane.WARNING_MESSAGE);
        }else if(!yzm.equals(yjtf.getText()))
        {
            JOptionPane.showMessageDialog(null,"验证码错误！","提示消息",JOptionPane.ERROR_MESSAGE);
            clear1();
            int j=4;
            String s="";
            yzm="";
            while(j-->0) {

                if(j%2==0) {
                    int x=(int) (Math.random()*10);
                    yzm=yzm+String.valueOf(x);
                    s=s+String.valueOf(x)+" ";
                }
                else {
                    char c=(char) ((97+(int) (Math.random()*26)));
                    yzm=yzm+String.valueOf(c);
                    s=s+String.valueOf(c)+" ";
                }
            }
            sjtf.setText(s);
        }else
        {
            JOptionPane.showMessageDialog(null,"用户名或者密码错误！\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
            //清空输入框
            clear();
        }
    }
    //教师登录判断方法
    @SuppressWarnings("deprecation")
    public void tealogin()
    {
        if(tea_name.equals(jtf.getText())&&tea_pwd.equals(jpf.getText())&&yzm.equals(yjtf.getText())&&yzm.equals(yjtf.getText()))
        {
            //System.out.println("登录成功");
            JOptionPane.showMessageDialog(null,"登录成功！","提示消息",JOptionPane.WARNING_MESSAGE);
            clear();
            new Main();
        }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(null,"请输入用户名和密码！","提示消息",JOptionPane.WARNING_MESSAGE);
        }else if(jtf.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(null,"请输入用户名！","提示消息",JOptionPane.WARNING_MESSAGE);
        }else if(jpf.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(null,"请输入密码！","提示消息",JOptionPane.WARNING_MESSAGE);
        }else if(yjtf.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(null,"请输入验证码！","提示消息",JOptionPane.WARNING_MESSAGE);
        }else if(!yzm.equals(yjtf.getText()))
        {
            JOptionPane.showMessageDialog(null,"验证码错误！","提示消息",JOptionPane.ERROR_MESSAGE);
            clear1();
            int j=4;
            String s="";
            yzm="";
            while(j-->0) {

                if(j%2==0) {
                    int x=(int) (Math.random()*10);
                    yzm=yzm+String.valueOf(x);
                    s=s+String.valueOf(x)+" ";
                }
                else {
                    char c=(char) ((97+(int) (Math.random()*26)));
                    yzm=yzm+String.valueOf(c);
                    s=s+String.valueOf(c)+" ";
                }
            }
            sjtf.setText(s);
        }else
        {
            JOptionPane.showMessageDialog(null,"用户名或者密码错误！\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
            //清空输入框
            clear();
        }
    }
    //清空文本框和密码框
    public void clear()
    {
        jtf.setText("");
        jpf.setText("");
    }
    public void clear1() {
        jpf.setText("");
        yjtf.setText("");
    }

}